<template>
    <base-breadcrumb>
        <searchData :searchData="searchArr" @search="search"></searchData>

        <a-card>
            <div class="table-operator">
                <a-button type="primary" icon="plus" @click="handleAdd" v-btnPermission="'bm_010_add_01'"
                    >新增</a-button
                >
            </div>
            <div class="table-operateors mb20">
                <div class="pub-table-select-count" style="margin: 10px 0">
                    <a-icon type="info-circle" theme="twoTone" />
                    <span>
                        至本月累计金额(不含税)(万元):
                        <a>{{ from.amountNoTax }}</a
                        >，
                    </span>
                    <span>
                        至本月累计税金(万元):
                        <a>{{ from.tax }}</a
                        >，
                    </span>
                    <span>
                        至本月累计金额(含税)(万元)
                        <a>{{ from.amountTax }}</a>
                    </span>
                </div>
            </div>
            <s-table
               
                bordered
                ref="table"
                size="default"
                rowKey="key"
                :columns="columns"
                :data="loadData"
                :alert="true"
                show-size-changer
                show-quick-jumper
                showPagination="auto"
            >
                <span slot="operation" slot-scope="text, record">
                    <a @click="detailed(record)" v-btnPermission="'bm_010_loo_04'">查看</a>
                    <a-divider v-if="isEdit(record)" type="vertical" v-btnPermission="'bm_010_edi_02'" />
                    <a v-if="isEdit(record)" @click="addEdit(record)" v-btnPermission="'bm_010_edi_02'">编辑</a>

                    <!-- <a v-show="record.step != '1'" @click="getProcessNodes(record)">部门意见</a> -->
                    <span v-if="record.step != '1'" v-btnPermission="'bm_010_moi_05'">
                        <a-divider type="vertical" />
                        <approval-button
                            :status="
                                record.auditStatus == 'SIGNSTATUS' || record.step != '1'
                                    ? 'SUBMITTED'
                                    : record.auditStatus
                            "
                            :id="record.measureId"
                            v-btnPermission="'bm_009_moi_05'"
                        ></approval-button>
                    </span>
                    <!-- <a @click=";(visible = true), (businessId = record.id)">审批详情</a> -->
                </span>
                <span slot="step" slot-scope="text, record">
                    <span v-show="record.step == '0'">已完成</span>
                    <span v-show="record.step == '1'">基本信息填写</span>
                    <span v-show="record.step == '2'">部门意见填写</span>
                    <span v-show="record.step == '3'">资料汇总</span>
                    <span v-show="record.step == '4'">上传签字盖章后的资料</span>
                </span>
                <template slot="auditStatus" slot-scope="text, record">
                    <list-status
                        :status="record.auditStatus"
                        :step="record.step"
                        :isApproval="true"
                        conversion="en"
                        :text="text"
                    ></list-status>
                </template>
            </s-table>
        </a-card>
        <!--    
            <a-form-model
                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                :wrapperCol="{ lg: { span: 16 }, sm: { span: 16 } }"
            >
                <s-table
                    bordered
                    ref="commentsTable"
                    size="default"
                    rowKey="key"
                    :data="comments"
                    :columns="columnApprove"
                    :alert="true"
                    show-size-changer
                    show-quick-jumper
                    :showPagination="false"
                >
                    <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
                </s-table>
            </a-form-model>
        </a-modal> -->
        <!-- 审批详情 -->
        <!-- <ApprovalDetails
            v-if="visible"
            :title="'审批详情'"
            :businessId="businessId"
            :visible="visible"
            @cancel="visible = false"
            @confirm="visible = false"
        /> -->
    </base-breadcrumb>
</template>

<script>
import { getSmlList, getStatistical } from '@/api/businessAffairs/sml'
import { themeChoose } from '@/mixin/mixin' // mixin

import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商
import { STable } from '@/components'
import { getProcessNodeList, completeTask } from '@/api/process'
import { getOrgEmployeesListBy } from '@/api/commons'
import ApprovalDetails from '@/components/approvalDetails'
const columns = [
    // {
    //     title: '名称',
    //     dataIndex: 'name',
    // },
    {
        title: '计量月份',
        dataIndex: 'measureMonth',
       
    },
    {
        title: '分包单位',
        dataIndex: 'subContractorName',
       
    },
    {
        title: '所属分公司',
        dataIndex: 'branchComName',
        
    },
    {
        title: '所属项目',
        dataIndex: 'projectName',
        
    },

    {
        title: '本月(不含税)(万元)',
        dataIndex: 'amountExcTax',
        
    },
    {
        title: '本月(税金)(万元)',
        dataIndex: 'tax',
        
    },
    {
        title: '本月(含税)(万元)',
        dataIndex: 'amountAndTax',
        
    },
    {
        title: '至本月累计金额(含税)(万元)',
        dataIndex: 'abLastMonthAmount',
        width: '10%',
        
    },
    {
        title: '填报人',
        dataIndex: 'creatorName',
        
    },
    {
        title: '填报时间',
        dataIndex: 'createTime',
        
    },
    {
        title: '步骤',
        dataIndex: 'step',
        scopedSlots: { customRender: 'step' },
        
    },
    {
        title: '状态',
        dataIndex: 'auditStatus',
        scopedSlots: { customRender: 'auditStatus' },
       
    },
    {
        title: '操作',
        dataIndex: 'operation',
        width: '170',
       

        scopedSlots: { customRender: 'operation' },
    },
]
const searchArr = [
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
    // {
    //     name: '所属分公司',
    //     valueKey: 'branchComId',
    //     type: SelectSubCompany,
    // },
    // {
    //     name: '所属项目',
    //     keyValue: 'branchComId',
    //     valueKey: 'projectId',
    //     type: SelectProject,
    // },
    {
        name: '分包商名称',
        keyValue: 'projectId',
        valueKey: 'subContractorId',
        type: SelectSubcontractor,
    },
    {
        name: '施工范围',
        value: 'constructionRange',
        type: 'input',
    },
    {
        name: '填报人',
        value: 'creatorName',
        type: 'input',
    },
    {
        name: '填报时间',
        value: 'startEndDate',
        type: 'dateRange',
    },
    {
        name: '计量月份',
        value: 'mouth',
        type: 'month',
    },
    // {
    //     name: '状态',
    //     value: 'statusCode',
    //     type: 'select',
    //     data: [
    //         {
    //             value: '0',
    //             label: '草稿',
    //         },
    //         {
    //             value: '1',
    //             label: '已提交',
    //         },
    //         {
    //             value: '2',
    //             label: '通过',
    //         },
    //         {
    //             value: '3',
    //             label: '驳回',
    //         },
    //     ],
    // },
]
const columnApprove = [
    {
        title: '序号',
        width: 80,
        scopedSlots: { customRender: 'serial' },
    },
    {
        title: '审批人',
        dataIndex: 'userName',
    },
    {
        title: '审批时间',
        dataIndex: 'createTime',
    },
    {
        title: '岗位名称',
        dataIndex: 'taskName',
    },
    {
        title: '滞留时间',
        dataIndex: 'durationInMillis',
    },
    {
        title: '岗位意见',
        dataIndex: 'formData.opinions',
    },
]
export default {
    name:'a'+Date.now(),
    components: {
        STable,
        ApprovalDetails,
    },
    data() {
        this.columns = columns
        this.searchArr = searchArr
        this.columnApprove = columnApprove
        return {
            from: {},
            dataSource: [],
            visible: false,
            businessId: '639306335778902016',
            queryParam: {},
            approveInfos: [],
            // comments: (parameter) => {
            //     return new Promise((resolve, reject) => {
            //         resolve({
            //             data: this.approveInfos,
            //         })
            //     })
            // },
            loadData: (parameter) => {
                const requestParameters = Object.assign({}, parameter, this.queryParam)

                if (this.queryParam.startEndDate?.length > 0) {
                    requestParameters.createTimeBegin =
                        this.queryParam.startEndDate[0].format('YYYY-MM-DD HH:mm:ss') || void 0
                    requestParameters.createTimeEnd =
                        this.queryParam.startEndDate[1].format('YYYY-MM-DD HH:mm:ss') || void 0
                }

                return getSmlList(requestParameters).then((res) => {
                    console.log('数据返回对象：', res)
                    return res
                })
            },
        }
    },
    mounted() {
        this.getStatistical()
    },

    methods: {
        getStatistical() {
            const requestParameters = Object.assign(this.queryParam)
            if (this.queryParam.startEndDate?.length > 0) {
                requestParameters.createTimeBegin =
                    this.queryParam.startEndDate[0].format('YYYY-MM-DD HH:mm:ss') || void 0
                requestParameters.createTimeEnd =
                    this.queryParam.startEndDate[1].format('YYYY-MM-DD HH:mm:ss') || void 0
            }
            getStatistical(requestParameters).then((res) => {
                this.from = res.data
            })
        },
        isEdit(record) {
            if (record.auditStatus == 'SIGNSTATUS' && record.step == '2') {
                return false
            }
            if (record.auditStatus == 'SUBMITTED' && record.step == '3') {
                return false
            }
            if (record.step == '0') {
                return false
            }
            return true
        },
        //新增
        handleAdd() {
            this.$router.push('/businessAffairs/subcontractMeasurement/sml/basicInfoInput')
        },
        // 查看
        detailed(e) {
            this.$router.push({
                path: '/businessAffairs/subcontractMeasurement/sml/smDetail',
                query: { id: e.measureId },
            })
        },
        //编辑
        addEdit(e) {
            if ((e.step == '2' && e.auditStatus == 'PAST') || (e.step == '3' && e.auditStatus == 'PAST')) {
                e.step += 1
            }
            switch (e.step) {
                case 1:
                    this.$router.push({
                        path: '/businessAffairs/subcontractMeasurement/sml/basicInfoInput',
                        query: { id: e.measureId },
                    })
                    break
                case 2:
                    // this.$router.push({
                    //     path: '/businessAffairs/subcontractMeasurement/sml/summarInformation',
                    //     query: { id: e.measureId },
                    // })
                    break
                case 3:
                    this.$router.push({
                        path: '/businessAffairs/subcontractMeasurement/sml/summarInformation',
                        query: { id: e.measureId },
                    })
                    break
                case 4:
                    this.$router.push({
                        path: '/businessAffairs/subcontractMeasurement/sml/smComplete',
                        query: { id: e.measureId },
                    })
                    break

                default:
                    break
            }
        },
        getProcessNodes(e) {
            this.visible = true
            console.log(e)
            // return
            getProcessNodeList({ businessId: e.measureId, pageNo: 1, pageSize: 100 })
                .then((res) => {
                    if (res.code === 200) {
                        const data = res.data
                        const userIds = data.map((d) => {
                            if (d.formData) {
                                d.formData = JSON.parse(d.formData)
                            }
                            return d.assignee
                        })
                        getOrgEmployeesListBy({ userIds }).then((employees) => {
                            // this.flowRequesting = false
                            res.data.forEach((r) => {
                                employees.data.some((e) => {
                                    if (e.userId === r.assignee) {
                                        r.userName = e.name
                                        return true
                                    }
                                })
                            })
                            this.approveInfos = res.data
                            this.$refs.commentsTable.refresh(true)
                        })
                    }
                })
                .catch(() => {
                    // this.flowRequesting = false
                })
        },

        //搜索
        search(value) {
            console.log(value)
            this.queryParam = value
            this.getStatistical()
            this.$refs.table.refresh(true)
        },
    },
}
</script>
<style>
.pub-list-draft::before,
.pub-list-audit::before,
.pub-list-pass::before,
.pub-list-rejected::before,
.pub-list-del::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 10px;
}
.pub-list-audit::before {
    background-color: rgb(16, 141, 233);
}
.pub-list-pass::before {
    background-color: rgb(0, 168, 84);
}
.pub-list-rejected::before {
    background-color: rgb(254, 85, 0);
}
.pub-list-draft::before {
    background-color: rgb(255, 255, 0);
}
.pub-list-del::before {
    background-color: rgb(173, 173, 173);
}
</style>